package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.leaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.jshell.Snippet;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;


    //分页查询房间信息
    @Override
    public Page<RoomItemVo> pageRoomItemVo(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return baseMapper.pageRoomItemVoMapper(roomItemVoPage,queryVo);
    }


    //新增或更新房间信息
    @Override
    public void saveOrUpdateRoomInfoSubmitVo(RoomSubmitVo roomSubmitVo) {
        //获取房间id,通过是否有id判断是更新还是新增
        Long id = roomSubmitVo.getId();
        this.saveOrUpdate(roomSubmitVo);
        //新增基本信息
        if(id!=null){
            //更新操作
            //删除基本信息
//            this.removeById(id);
            //根据房间id和所属类型删除图片信息
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,roomSubmitVo.getId()).eq(GraphInfo::getItemType,ItemType.ROOM));
            //删除属性信息
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,roomSubmitVo.getId()));
            //删除配套信息
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,roomSubmitVo.getId()));
            //删除标签信息
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,roomSubmitVo.getId()));
            //删除支付方式信息
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,roomSubmitVo.getId()));
            //删除租期信息
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId()));
        }

        //1.新增图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                //构建GraphInfo对象
                GraphInfo graphInfo = new GraphInfo();
                //设置图片名称
                graphInfo.setName(graphVo.getName());
                //设置图片路径url
                graphInfo.setUrl(graphVo.getUrl());
                //设置图片所属对象类型
                graphInfo.setItemType(ItemType.ROOM);
                //设置图片所属对象id
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //2.新增房间属性信息

        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            //创建关联表集合
            List<RoomAttrValue> roomAttrValueArrayList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValueArrayList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueArrayList);
        }
        //3.新增房间配套
        /*List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            //创建关联表集合
            List<RoomFacility> roomFacilitielist = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                roomFacilitielist.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilitielist);
        }*/
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();

                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        //4.新增房间标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if(!CollectionUtils.isEmpty(labelInfoIds)){
            //创建关联表集合
            List<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //5.新增支付方式信息列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        //6.新增可选租期列表信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }

    }

    //根据id查询房间详细信息
    @Override
    public RoomDetailVo getRoomDetailVoById(Long id) {
        //调用自带方法查询房间的基本信息
        RoomInfo roomInfo = this.getById(id);
        //查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //根据房间id和所属图片类型查询图片信息
       List<GraphVo> graphVoList =  graphInfoMapper.selectGraphVOByRoomIdAndItemTypeId(id,ItemType.ROOM);
       //根据房间id查询属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoByRoomId(id);
        //根据房间id查询配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityByRoomId(id);
        //查询标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLableByRoomId(id);
        //查询支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeByRoomId(id);
        //查询租期
        List<LeaseTerm> leaseTermList =leaseTermMapper.selectLeaseTermByRoomId(id);




        //转换成roomDetailVo
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        //设置公寓信息
        roomDetailVo.setApartmentInfo(apartmentInfo);
        //设置图片信息
        roomDetailVo.setGraphVoList(graphVoList);
        //设置属性信息
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        //设置配套信息
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        //设置标签
        roomDetailVo.setLabelInfoList(labelInfoList);
        //设置支付方式
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        //设置租期
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    //根据id删除房间信息
    @Override
    public void removeRoomInfoById(Long id) {

        //判断房间是否还有租约
        //查询房间对应的租约信息
        LambdaQueryWrapper<LeaseAgreement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LeaseAgreement::getRoomId,id).in(LeaseAgreement::getStatus,2,5,7);
        Long leaseCount = leaseAgreementMapper.selectCount(lambdaQueryWrapper);
        if(leaseCount!=0){
            throw new leaseException(ResultCodeEnum.ROOM_HAS_LEASE);
        }
        //删除房间信息
        this.removeById(id);

        //根据房间id和所属类型删除图片信息
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.ROOM));
        //删除属性信息
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        //删除配套信息
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        //删除标签信息
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        //删除支付方式信息
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        //删除租期信息
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));



    }
}




